package com.hs.gs.web;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;

import com.hs.gs.domain.DynFile;

@RooWebScaffold(path = "dynfiles", formBackingObject = DynFile.class, update = false)
@RequestMapping("/dynfiles")
@Controller
public class DynFileController {
	
	private static final Log log = LogFactory.getLog(DynFileController.class);

	@InitBinder
	protected void initBinder(HttpServletRequest request,
	                       ServletRequestDataBinder binder)
	                   throws ServletException {
	    	binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
	}
	@RequestMapping(value="savedoc",  method = RequestMethod.POST)
	public String createdoc(@Valid DynFile document,
                    BindingResult result,
                    Model model,
                    @RequestParam("content") MultipartFile content,
                    HttpServletRequest request) {
 
//	        document.setContentType(content.getContentType());
//	        document.setFilename(content.getOriginalFilename());
//	        document.setSize(content.getSize());
 
        log.debug("Document: ");
        log.debug("Name: "+content.getOriginalFilename());
//	        log.debug("Description: "+document.getDescription());
        log.debug("File: " +content.getName());
        log.debug("Type: "+content.getContentType());
        log.debug("Size: "+content.getSize());
        if (result.hasErrors()) {
            model.addAttribute("dynfile", document);
            return "dynfiles/create";
        }
        document.persist();
 
        return "redirect:/dynfiles?page=1&size=10" + encodeUrlPathSegment(document.getId().toString(), request);
    }
	 
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String show(@PathVariable("id") Long id, Model model) {
	DynFile doc = DynFile.findDynFile(id);
//	        doc.setU("/documents/showdoc/"+id);
        model.addAttribute("document", DynFile.findDynFile(id));
        model.addAttribute("itemId", id);
        return "dynfiles/show";
    }
	 
	@RequestMapping(value = "/showdoc/{id}", method = RequestMethod.GET)
	public String showdoc(  @PathVariable("id") Long id,
	                    HttpServletResponse response,
	                    Model model) {
		DynFile doc = DynFile.findDynFile(id);
	 
	   try {
	          response.setHeader("Content-Disposition", "inline;filename=\"" +doc.getName()+ "\"");
	 
	          OutputStream out = response.getOutputStream();
//	          response.setContentType(doc.getContentType());
//	          IOUtils.copy( new ByteArrayInputStream(doc.getContent()),out);
	            out.flush();
	 
	        } catch (IOException e) {
	            e.printStackTrace();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return null;
	    }
}
